<template>
  <div class="containerHall">
    <el-dialog
      :title="dialogConfig.name"
      :visible.sync="dialogVisible"
      @close="dialogVisible = false"
      append-to-body
    >
      <el-form ref="formRef" :model="formData" :rules="rules">
        <el-form-item label="uid" :label-width="'100px'" prop="uid">
          <el-input v-model="formData.uid" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="展馆名称" :label-width="'100px'" prop="name">
          <el-input v-model="formData.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="链接" :label-width="'100px'" prop="link">
          <el-input v-model="formData.link" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="优先级" :label-width="'100px'" prop="seqencing">
          <el-input v-model="formData.seqencing" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="picture" label-width="100px">
          <ul class="el-upload-list" style="display: flex; flex-wrap: wrap">
            <li v-if="formData.image != ''">
              <el-image
                :src="formData.image | imageUrl"
                style="width: 100px; height: 100px; margin: 20px"
                :fit="'contain'"
                @click="handleImageClicked(pic)"
              >
              </el-image>
              <i
                class="el-icon-close"
                style="position: absolute"
                @click="handleRemoveImage()"
              ></i>
            </li>
          </ul>
          <el-upload
            action="/console/api/upload/image"
            :show-file-list="false"
            :multiple="true"
            :on-success="handlePictureOK"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件</el-button
            >
          </el-upload>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>

      <el-dialog :visible.sync="imageDialogVis" append-to-body>
        <el-image :fit="'contain'" :src="imageToView"></el-image>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { hallUpdateApi, hallAddApi } from '@api/pop'
export default {
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    value: {
      type: Object
    },
    request: {
      type: Object
    },
    dialogConfig: {
      type: Object
    }
  },
  data() {
    return {
      posting: false,
      formData: this.setup(this.value),
      rules: {},
      imageToView: null,
      imageDialogVis: false
    }
  },
  created() {},
  mounted() {},
  methods: {
    setup() {
      if (!this.value) {
        return {
          id: 0,
          image: '',
          link: '',
          name: '',
          seqencing: 1,
          uid: 0
        }
      } else {
        const clone = deepClone(this.value)
        return clone
      }
    },
    handleConfirm() {
      this.posting = true
      let func = null
      if (this.formData.id) {
        func = hallUpdateApi
      } else {
        func = hallAddApi
      }
      const clone = deepClone(this.formData)
      func(clone)
        .then(res => {
          this.dialogVisible = false
          this.$emit('success') // 刷新
        })
        .catch(err => {
          this.$message('error')
        })
    },

    // 上传图片
    handlePictureOK(pic) {
      this.formData.image = pic.data.url
    },
    handleRemoveImage(pictures, i) {
      this.formData.image = ''
    },
    handleImageClicked(pic) {
      this.imageToView = pic
      this.imageDialogVis = true
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.formData = this.setup()
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.containerHall {
}
</style>
